package com.yh.cms.service;

import com.lxw.framework.domain.cms.CmsPage;
import com.lxw.framework.domain.cms.CmsSite;
import com.lxw.framework.domain.cms.response.CmsCode;
import com.lxw.framework.exception.CastException;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSDownloadStream;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.yh.cms.dao.CmsPageRepository;
import com.yh.cms.dao.CmsSiteRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsResource;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Optional;

@Service
@Slf4j
public class PageService {

    @Autowired
    private CmsPageRepository cmsPageRepository;
    @Autowired
    private GridFsTemplate gridFsTemplate;

    @Resource
    private GridFSBucket gridFSBucket;
    @Autowired
    private CmsSiteRepository cmsSiteRepository;

    public void storeHtml2Path(String pageId){
        if(StringUtils.isEmpty(pageId)){
            CastException.cast(CmsCode.CMS_PAGE_EXISTSID);
        }
        //获取cmsPage对象
        Optional<CmsPage> cmsPageOptional = cmsPageRepository.findById(pageId);
        if(!cmsPageOptional.isPresent()){
            CastException.cast(CmsCode.CMS_PAGE_EXISTSID);
        }
        CmsPage cmsPage = cmsPageOptional.get();
        this.downloadHtml(cmsPage);
    }
    //把html页面从gridFs上下载下来，存放到指定未知
    public void downloadHtml(CmsPage cmsPage){

        String htmlFileId = cmsPage.getHtmlFileId();

        //
        GridFSFile gridFSFile = gridFsTemplate.findOne(Query.query(Criteria.where("_id").is(htmlFileId)));
        //
        GridFSDownloadStream gridFSDownloadStream = gridFSBucket.openDownloadStream(gridFSFile.getObjectId());
        //
        GridFsResource gridFsResource = new GridFsResource(gridFSFile,gridFSDownloadStream);
        //
        try {
            InputStream inputStream = gridFsResource.getInputStream();
            //获取站点Id
            String siteId = cmsPage.getSiteId();
            Optional<CmsSite> cmsSiteOptional = cmsSiteRepository.findById(siteId);
            if(!cmsSiteOptional.isPresent()){
                CastException.cast(CmsCode.CMS_PAGE_EXISTSID);
            }
            CmsSite cmsSite = cmsSiteOptional.get();
            //获取站点物理路径
            String path = cmsSite.getSitePhysicalPath()+cmsPage.getPagePhysicalPath()+cmsPage.getPageName();
            //
            FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
            IOUtils.copy(inputStream,fileOutputStream);
            fileOutputStream.close();
            inputStream.close();
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
    }
}
